package com.hellohoper.assembly.common.utils.base;

import com.hellohoper.assembly.common.utils.util.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by hk on 9/30/16.
 */
public class BaseController {

    private static final Logger logger = LoggerFactory.getLogger(BaseController.class);

    protected void printResult(String result, HttpServletResponse response) {
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
            writer.print(result);
        } catch (IOException e) {
            logger.error("写出结果失败!", e);
        } finally {
            if (writer != null)
                writer.close();
        }
    }

    protected Object getVFromSession(HttpServletRequest request, String key) {
        return request.getSession().getAttribute(key);
    }

    protected void setVToSession(HttpServletRequest request, String key, Object value) {
        request.getSession().setAttribute(key, value);
    }

    protected void addCookie(HttpServletResponse response, String name, String value) {
        this.addCookie(response, name, value, -1, "hellohoper.com");
    }

    protected void addCookie(HttpServletResponse response, String name, String value, int maxAge, String domain) {
        try {
            Cookie cookie = new Cookie(name, value);
            if (maxAge > 0)
                cookie.setMaxAge(maxAge);
            cookie.setPath("/");
            cookie.setDomain(domain);
            response.addCookie(cookie);
        } catch (Exception ex) {
            logger.error("创建Cookies发生异常！" + ex.getMessage());
        }
    }

    protected boolean clearCookie(HttpServletRequest request, HttpServletResponse response, String name) {
        boolean bool = false;
        Cookie[] cookies = request.getCookies();
        try {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = new Cookie(name, null);
                cookie.setMaxAge(0);
                cookie.setPath("/");//根据你创建cookie的路径进行填写
                response.addCookie(cookie);
                bool = true;
            }
        } catch (Exception ex) {
            logger.error("清空Cookies发生异常！");
        }
        return bool;
    }

    protected boolean clearCookie(HttpServletRequest request, HttpServletResponse response, String name, String domain) {
        boolean bool = false;
        Cookie[] cookies = request.getCookies();
        try {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = new Cookie(name, null);
                cookie.setMaxAge(0);
                cookie.setPath("/");//根据你创建cookie的路径进行填写
                cookie.setDomain(domain);
                response.addCookie(cookie);
                bool = true;
            }
        } catch (Exception ex) {
            logger.error("清空Cookies发生异常！");
        }
        return bool;
    }

    protected String getCookie(HttpServletRequest request, String name) {
        Cookie[] cookies = request.getCookies();
        String string = null;
        try {
            if (cookies != null) {
                for (int i = 0; i < cookies.length; i++) {
                    Cookie cookie = cookies[i];
                    String cName = cookie.getName();
                    if (cName != null && cName.equals(name)) {
                        string = cookie.getValue();
                    }

                }
            }
        } catch (Exception ex) {
            logger.error("获取Cookies发生异常！");
        }
        return string;
    }

    protected String callback(HttpServletRequest request, Object obj) {
        String callback = request.getParameter("callback");
        if (StringUtils.isBlank(callback)) {
            return null;
        } else {
            return callback + "(" + JsonUtils.toJson(obj) + ")";
        }
    }
}
